package cn.tedu._07springaop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
public class LogAspect {

//    @Pointcut("execution(public int cn.tedu._07springaop.aop.Calculator.*(..))")
//    @Pointcut("execution(public int cn.tedu._07springaop.aop.Calculator.add(..)) || " +
//            "execution(public int cn.tedu._07springaop.aop.Calculator.sub(..))")
//    @Pointcut("bean(calculatorImpl)")
    @Pointcut("within(cn.tedu._07springaop.aop.*)")
    public void pointcut() {

    }

//    @Before("execution(public int cn.tedu._07springaop.aop.Calculator.*(..))")
//    public void before(JoinPoint joinPoint) {
//        String name = joinPoint.getSignature().getName();
//
//        String args = Arrays.toString(joinPoint.getArgs());
//        System.out.println("[前置通知]" + name + "方法开始了，参数为" + args);
//    }
//
//    @After("execution(public int cn.tedu._07springaop.aop.Calculator.*(..))")
//    public void after(JoinPoint joinPoint) {
//        String name = joinPoint.getSignature().getName();
//        System.out.println("[后置通知]" + name + "方法结束了");
//    }
//
//    @AfterThrowing(value = "pointcut()",throwing = "ex")
//    public void afterThrowing(JoinPoint joinPoint,Exception ex) {
//        String name = joinPoint.getSignature().getName();
//        System.out.println("[异常通知]" + name + "方法出现异常了,提示消息为:" + ex.getMessage());
//    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint){
        Object proceed = null;

        try {
            System.out.println("[环绕通知]-前置通知");
            proceed = joinPoint.proceed();
            System.out.println("[环绕通知]-返回通知");
        } catch (Throwable e) {
            System.out.println("[环绕通知]-异常通知");
            throw new RuntimeException(e);
        } finally {
            System.out.println("[环绕通知]-后置通知");
        }

        return proceed;
    }
}
